package com.example.ui.homepage;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.example.huashangapp.R;
import com.example.util.BitmapLoad;

public class HomePageGalleryAdapter extends BaseAdapter {
	
	private Context mContext;
	private List<Map<String, Object>> imgList;
	
	public HomePageGalleryAdapter(Context context,List<Map<String, Object>> imgWallList){
		mContext = context;
		imgList = imgWallList;
	}

	@Override
	public int getCount() {
		if(imgList.size()==0){
			//return galleryArr[0];
			HomePageView.imgWall.setVisibility(View.GONE);
			HomePageView.imgWall0Img.setVisibility(View.VISIBLE);
			return 0;
		}
		HomePageView.imgWall0Img.setVisibility(View.GONE);
		HomePageView.imgWall.setVisibility(View.VISIBLE);
		return imgList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@SuppressWarnings("deprecation")
	@SuppressLint("InlinedApi")
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Holder holder = null;
		if(convertView == null){
			holder = new Holder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.homepage_gallery_img, null);
			holder.img = (ImageView)convertView.findViewById(R.id.homepage_gallery_img);
			convertView.setTag(holder);
		}else{
			holder=(Holder)convertView.getTag();
			
		}
		
		
		try {
			Bitmap bit = null;
			
			String imgUrl = (String)imgList.get(position).get("photo").toString();
			if(imgUrl!=null){
				bit=BitmapLoad.loadBitmap(mContext,imgUrl);
				Drawable dra = new BitmapDrawable(bit);
				holder.img.setBackgroundDrawable(dra);
			}else{
				holder.img.setBackgroundResource(R.drawable.wall_bg);
			}
			
			holder.img.setScaleType(ImageView.ScaleType.CENTER);
			holder.img.setAdjustViewBounds(true);
			holder.img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return convertView;  
	}

	
	class Holder {
		ImageView img;
	}
	
	

}
